import { dbUtils } from '../core'

export interface StoreShopDeskOrderFoodRow {
    id: string
    store_id: string
    shop_id: string
    clerk_id: string
    desk_id: string
    record_id: string
    is_write_food: number
    write_food_name: string
    category_id: string
    food_sku_id: string
    order_id: string
    num: number
    unclosing_num: number
    desk_num: number
    is_weight: number
    edit_price: number
    price: number
    member_price: number
    model_price: number
    is_vip: number
    write_making: string
    total_making_money: number
    write_taste: string
    position: string
    order_discount: number
    member_discount: number
    discount: number
    model_discount: number
    discount_type: number
    un_discount_ratio: number
    money: number
    total_money: number
    unclosing_total_money: number
    is_present: number
    is_group: number
    present_num: number
    belong_type: number
    is_after_add: number
    status: number
    wait_time: number
    print: number
    is_unclosing: number
    remarks: string
    is_delete: number
    create_time: number
    update_time: number
}

export const storeShopDeskOrderFoodRepository = {
    upsertBatch(rows: StoreShopDeskOrderFoodRow[]): void {
        if (!rows || rows.length === 0) return
        dbUtils.transaction(() => {
            const cols = [
                'id', 'store_id', 'shop_id', 'clerk_id', 'desk_id', 'record_id', 'is_write_food', 'write_food_name', 'category_id',
                'food_sku_id', 'order_id', 'num', 'unclosing_num', 'desk_num', 'is_weight', 'edit_price', 'price', 'member_price',
                'model_price', 'is_vip', 'write_making', 'total_making_money', 'write_taste', 'position', 'order_discount',
                'member_discount', 'discount', 'model_discount', 'discount_type', 'un_discount_ratio', 'money', 'total_money',
                'unclosing_total_money', 'is_present', 'is_group', 'present_num', 'belong_type', 'is_after_add', 'status',
                'wait_time', 'print', 'is_unclosing', 'remarks', 'is_delete', 'create_time', 'update_time'
            ]
            const placeholders = cols.map(() => '?').join(', ')
            const updateSet = cols.filter(c => c !== 'id').map(c => `${c}=excluded.${c}`).join(', ')
            const sql = `INSERT INTO store_shop_desk_order_food (${cols.join(', ')}) VALUES (${placeholders})
            ON CONFLICT(id) DO UPDATE SET ${updateSet}, updated_at=CURRENT_TIMESTAMP`

            for (const r of rows) {
                // console.log('当前ID:', r.id + ' 当前行数:', rows.indexOf(r) + 1 + '当前长度：' + Object.keys(r).length);

                const d = (k: keyof StoreShopDeskOrderFoodRow, def: any) => (r[k] ?? def)
                const vals = cols.map((c) => d(c as keyof StoreShopDeskOrderFoodRow, (
                    c === 'desk_num' ? 1 :
                        c === 'is_weight' ? 0 :
                            c === 'edit_price' ? 0 :
                                c === 'price' ? 0 :
                                    c === 'member_price' ? 0 :
                                        c === 'model_price' ? 0 :
                                            c === 'is_vip' ? 0 :
                                                c === 'total_making_money' ? 0 :
                                                    c === 'order_discount' ? 10 :
                                                        c === 'member_discount' ? 10 :
                                                            c === 'discount' ? 10 :
                                                                c === 'model_discount' ? 10 :
                                                                    c === 'discount_type' ? 0 :
                                                                        c === 'un_discount_ratio' ? 0 :
                                                                            c === 'money' ? 0 :
                                                                                c === 'total_money' ? 0 :
                                                                                    c === 'unclosing_total_money' ? 0 :
                                                                                        c === 'is_present' ? 0 :
                                                                                            c === 'is_group' ? 0 :
                                                                                                c === 'present_num' ? 0 :
                                                                                                    c === 'belong_type' ? 1 :
                                                                                                        c === 'is_after_add' ? 0 :
                                                                                                            c === 'status' ? 1 :
                                                                                                                c === 'wait_time' ? 0 :
                                                                                                                    c === 'print' ? 1 :
                                                                                                                        c === 'is_unclosing' ? 0 :
                                                                                                                            c === 'is_delete' ? 0 : 0
                )))
                if (vals.length !== cols.length) {
                    console.error('[DeskOrderFood] values length mismatch:', vals.length, 'expected:', cols.length)
                }
                dbUtils.execute(sql, vals)
            }
        })
    }
}

export default storeShopDeskOrderFoodRepository


